package com.example.jdbc;

import java.sql.*;
import java.util.Locale;
import java.util.Objects;
import java.util.ResourceBundle;

/**
 * 配置外部化
 */
public class Demo3 {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet rs = null;
        ResourceBundle jdbc = ResourceBundle.getBundle("jdbc",new Locale("zh", "CN"));
        String driver = jdbc.getString("driver");
        String url = jdbc.getString("url");
        String user = jdbc.getString("user");
        String password = jdbc.getString("password");
        try {
            // 注册驱动
            Class.forName(driver);
            // 获取连接
            connection = DriverManager.getConnection(url, user, password);
            // 获取数据库操作对象
            statement = connection.createStatement();
            // 执行SQL
            rs = statement.executeQuery("select 1 from dual");
            // 处理查询结果
            // ***
        } catch (Exception e) {
            e.printStackTrace();
        } finally { // 释放资源
            if (Objects.nonNull(rs)) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (Objects.nonNull(statement)) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (Objects.nonNull(connection)) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
}
